package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.LoginActivity;
import com.wude.guild.R;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;
import com.wude.tools.ViewUtils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public class ModifyPasswordFragment extends TitleBaseFragment {
    private Unbinder unbinder;

    @BindView(R.id.edt_yuan)
    EditText edtYuan;
    @BindView(R.id.edt_new)
    EditText edtNew;
    @BindView(R.id.edt_agin)
    EditText edtAgin;
    @BindView(R.id.ok)
    TextView ok;
    private String teyuan;
    private String tenew;
    private String teagin;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_modify_password, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ModifyPasswordFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ModifyPasswordFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        setHeaderTitle("修改密码");

        ViewUtils.setEditTextInhibitInputSpace(edtNew);
        ViewUtils.setEditTextInhibitInputSpace(edtYuan);
        ViewUtils.setEditTextInhibitInputSpace(edtAgin);
    }

    @OnClick({R.id.ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok:
                UpdataPass();
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void UpdataPass() {
        teyuan = edtYuan.getText().toString();
        tenew = edtNew.getText().toString();
        teagin = edtAgin.getText().toString();
        if (teyuan.equals("")) {
            ToastUtil.showToast("请输入原密码");
            return;
        }
        if (tenew.equals("")) {
            ToastUtil.showToast("请输入新密码");
            return;
        }
        if (teagin.equals("")) {
            ToastUtil.showToast("请再次输入密码");
            return;
        }
        if (!tenew.equals(teagin)) {
            ToastUtil.showToast("两次输入不一致");
            return;
        }

        if (tenew.length() < 6 || teagin.length() < 6) {
            ToastUtil.showToast("密码长度不能少于6位");
            return;
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("old_pwd", Utils.getBase64String(teyuan.getBytes(), 0));
        map.put("new_pwd", Utils.getBase64String(tenew.getBytes(), 0));
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(handler, HttpCom.getPasswordURL(), map, false);
    }


    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("修改密码json", msg.obj.toString());
                    boolean b = HttpUtils.DNSChagePass(msg.obj.toString());
                    if (b) {
                        try {
                            DbManager db = DbUtils.getDB();
                            db.delete(UserInfo.class);
                            startActivity(new Intent(getContext(), LoginActivity.class));
                            getActivity().finish();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
